package com.automannn.atm_back.modules.codeGenerator.service.impl;

import com.automannn.atm_back.modules.codeGenerator.entity.ColumnConfigEntity;
import com.automannn.atm_back.modules.codeGenerator.entity.TypeMapperEntity;
import com.automannn.atm_back.modules.codeGenerator.mapper.TypeMapperEntityMapper;
import com.automannn.atm_back.modules.codeGenerator.service.TypeMapperEntityService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (GeneratorTypeMapper)表服务实现类
 *
 * @author chenkaihai
 * @since 2020-07-12 17:06:20
 */
@Service
public class TypeMapperEntityServiceImpl implements TypeMapperEntityService {
    @Resource
    private TypeMapperEntityMapper typeMapperEntityMapper;


    /**
     * 查询一个对象
     *
     * @param id return 实例对象
     */
    @Override
    public TypeMapperEntity queryOne(String id) {
        return this.typeMapperEntityMapper.queryById(id);
    }

    /**
     * 查询所有对象
     * <p>
     * return 实例对象
     */
    @Override
    public IPage<TypeMapperEntity> queryAll(Page<TypeMapperEntity> page, TypeMapperEntity generatorTypeMapper) {
        return this.typeMapperEntityMapper.queryAll(page, generatorTypeMapper);
    }

    /**
     * 新增数据
     *
     * @param generatorTypeMapper 实例对象
     * @return 实例对象
     */
    @Override
    public TypeMapperEntity insert(TypeMapperEntity generatorTypeMapper) {
        this.typeMapperEntityMapper.insert(generatorTypeMapper);
        return generatorTypeMapper;
    }

    /**
     * 修改数据
     *
     * @param generatorTypeMapper 实例对象
     * @return 实例对象
     */
    @Override
    public void update(TypeMapperEntity generatorTypeMapper) {
        typeMapperEntityMapper.update(generatorTypeMapper);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.typeMapperEntityMapper.deleteById(id) > 0;
    }

    @Override
    public void deleteBatchByIds(String ids) {
        typeMapperEntityMapper.deleteBatchIds(Arrays.asList(ids.split(",")));
    }

    @Override
    public void saveOrUpdate(List<TypeMapperEntity> typeMapperEntityList) {
        List<TypeMapperEntity>   toUpdateList=
                typeMapperEntityList.stream().filter(typeMapperEntity -> typeMapperEntity.getId() != null && typeMapperEntity.getId() != "").collect(Collectors.toList());
        List<TypeMapperEntity> toAddList =
                typeMapperEntityList.stream().filter(typeMapperEntity -> typeMapperEntity.getId() == null || typeMapperEntity.getId() == "").collect(Collectors.toList());
        toAddList.forEach(columnConfigEntity -> typeMapperEntityMapper.insert(columnConfigEntity));
        toUpdateList.forEach(
                columnConfigEntity -> typeMapperEntityMapper.updateById(columnConfigEntity));

    }
}
